#!/usr/bin/env python
# encoding: utf-8
"""
tablemsg.py

Created by 黄 冬 on 2007-12-19.
Copyright (c) 2007 __MyCompanyName__. All rights reserved.
"""
import logging.config

log = logging.getLogger('xbaytable.utils.tablemsg')

from xbaytable.conf import *
import binpack

class ProcessMessage(object):
    """处理消息体"""
    
    def recv_head(self,head):
        """收取消息头"""
        log.debug('recv head is %s'%repr(head))
        return binpack.unpackhead(head)

    def send_bind_table(self,transport,username,password):
        """向服务器发起bind请求，返回sequence_id"""
        from datetime import datetime
        now = datetime(1999,1,1).now()
        timestamp =  '%02i%02i%02i%02i%02i'%(now.month,now.day,now.hour,now.minute,now.second)
        #生成authentiator_code，长度为16位的MD5结果值
        import md5
        m=md5.new()
        m.update('%s%s%s'%(username,password,timestamp))
        authentiator_code=m.digest()
        sequence_id=get_sequence_id()
        #组包
        msg = binpack.genmsg(TABLE_BIND,sequence_id,username,authentiator_code,TABLE_VERSION,timestamp)
        #发送
        transport.sendString(msg)
        log.debug('bind_table_server send msg is %s'%repr(msg))
        return sequence_id

    def process_bind_table(self,transport,command,sequence_id,msg):
        """处理进来的bind_table请求"""
        user_name,authentiator_code,version,timestamp=binpack.unpackbody(command,msg)
        user_name = user_name.rstrip('\x00')
        log.debug("bind username is %s"%user_name)
        import md5
        m=md5.new()
        try:
            m.update('%s%s%s'%(user_name,TABLE_USER[user_name],timestamp))
            if (authentiator_code==m.digest()):
                #上来的哥们是自家人
                msg = binpack.genmsg(TABLE_BIND_RESP,sequence_id,0,TABLE_VERSION)
                transport.sendString(msg)
                return 0
        except KeyError:
            pass
        #上来的哥们认证不对哟
        msg = binpack.genmsg(TABLE_BIN_RESP,sequence_id,2,TABLE_VERSION)
        transport.sendString(msg)
        return 2

    def process_bind_table_resp(self,transport,command,sequence_id,msg):
        status,version=binpack.unpackbody(command,msg)
        if (status==0 and version==TABLE_VERSION):
            return 0
        return 1

    def send_table_set(self,transport,name,value):
        """向服务发送set包
        参数说明：
        name 要set的name
        value 对应name的值
        返回值：
        发送出去的包的sequence_id"""
        sequence_id=get_sequence_id()
        msg = binpack.genmsg(TABLE_SET,sequence_id,len(name),name,value)
        transport.sendString(msg)
        log.debug('send table_set is %s;seqid is %s'%(name,sequence_id))
        return sequence_id

    def process_table_set(self,transport,command,sequence_id,msg):
        """处理发到服务器端的table_set请求"""
        name_len,name,value=binpack.unpackbody(command,msg)
        log.debug('recv table_set is %s;seqid is %s'%(name,sequence_id))
        #不做处理，直接告诉哥们对了 :)
        msg = binpack.genmsg(TABLE_SET_RESP,sequence_id,0)
        transport.sendString(msg)

    def process_table_set_resp(self,transport,command,sequence_id,msg):
        """处理process_table_set_resp回复"""
        status = binpack.unpackbody(command,msg)
        log.debug('rev table_set_resp is %s'%status)

    def send_table_append(self,transport,name,value):
        """
        向服务器发送append数据包
        参数说明：
        name 要set的name
        value 对应name的追加值
        返回值：
        发送出去的包的sequence_id"""
        sequence_id=get_sequence_id()
        msg = binpack.genmsg(TABLE_APPEND,sequence_id,len(name),name,value)
        transport.sendString(msg)
        log.debug('send table_append is %s;seqid is %s'%(name,sequence_id))
        return sequence_id

    def process_table_append(self,transport,command,sequence_id,msg):
        """处理发到服务器端的table_append请求"""
        name_len,name,value=binpack.unpackbody(command,msg)
        log.debug('recv table_set is %s;seqid is %s'%(name,sequence_id))
        #不做处理，直接告诉哥们对了 :)
        msg = binpack.genmsg(TABLE_APPEND_RESP,sequence_id,0)
        transport.sendString(msg)

    def process_table_append_resp(self,transport,command,sequence_id,msg):
        """process_table_append_resp"""
        status = binpack.unpackbody(command,msg)
        log.debug('rev table_append_resp is %s'%status)

    def send_table_get(self,transport,name):
        """向服务器发送get包
        参数说明：
        name 要get的name
        返回值：
        发送出去的包的sequence_id"""
        sequence_id=get_sequence_id()
        msg = binpack.genmsg(TABLE_GET,sequence_id,name)
        transport.sendString(msg)
        log.debug('send table_get msg is %s'%repr(msg))
        return sequence_id

    def process_table_get(self,transport,command,sequence_id,msg):
        """处理发到服务器端的table_get请求"""
        name=binpack.unpackbody(command,msg)
        log.debug('recv table_get is %s;seqid is %s'%(name,sequence_id))
        #把名字加上123反回
        msg = binpack.genmsg(TABLE_GET_RESP,sequence_id,0,len(name),name,'%s123'%name)
        transport.sendString(msg)

    def process_table_get_resp(self,transport,command,sequence_id,msg):
        """process_table_get_resp"""
        status,name_len,name,value=binpack.unpackbody(command,msg)
        log.debug('rev table_get_resp is %s'%name)

    def send_table_del(self,transport,name):
        """
        向服务器发送del包
        参数说明：
        name 要del的name
        返回值：
        发送出去的包的sequence_id
        """
        sequence_id=get_sequence_id()
        msg = binpack.genmsg(TABLE_DEL,sequence_id,name)
        transport.sendString(msg)
        log.debug('send table_del msg is %s'%repr(msg))
        return sequence_id

    def process_table_del(self,transport,command,sequence_id,msg):
        """docstring for process_table_del"""
        pass

